#include "ENOilTankGroupHeatFluxMaterial.h"
#include <iostream>
#include "FEProblem.h"
#include "libmesh/elem.h"
#include "libmesh/plane.h"
#include "libmesh/quadrature.h"
#include "Assembly.h"

using namespace std;
registerMooseObject("RoshanApp", ENOilTankGroupHeatFluxMaterial);
template<>
InputParameters validParams<ENOilTankGroupHeatFluxMaterial>()
{
  InputParameters params = validParams<Material>();
  params += validParams<RandomInterface>();
  params.addCoupledVar("temperature", "温度场");
  params.addRequiredParam<UserObjectName>("oiltankUO", "oil tank group userobject");
  return params;
}

ENOilTankGroupHeatFluxMaterial::ENOilTankGroupHeatFluxMaterial(const InputParameters & parameters) :
      Material(parameters),
	_current_side_elem(_assembly.sideElem()),
	_temperature(coupledValue("temperature")),
	_OTuo(getUserObject<ENOilTankGroupHeatFluxUserObject>("oiltankUO")),
	_oil_tank_group_heat_flux(declareProperty<Real>("ENOilTankgroup_heat_flux")),
	_oil_tank_group_heat_flux_jacobi(declareProperty<Real>("ENOilTankgroup_heat_flux_jacobi"))
{
}

void ENOilTankGroupHeatFluxMaterial::computeQpProperties()
{
	vector<Real> OT_heat_flux_list = _OTuo.getENOilTankHeatFluxList();
	vector<Real> OT_heat_flux_jacobi_list = _OTuo.getENOilTankHeatFluxJacobiList();
	vector<Point> center_point_list = _OTuo.getCenterPointList();

	int all_element_size = center_point_list.size();
	int findi = -1;

	for (unsigned int i = 0; i < all_element_size; ++i)
	{
		if ( ( center_point_list[i] - _current_side_elem->centroid() ).norm() < 1e-20 )
		{
			findi = i;
			break;
		}
	}

	if (findi == -1)
	{
		cout << "Current sideelement center : " << _current_side_elem->centroid() << endl;
		mooseError("Cannot find i.");
	}

	for(_qp = 0; _qp < _qrule->n_points(); ++_qp)
	{
		_oil_tank_group_heat_flux[_qp] = OT_heat_flux_list[findi];
		_oil_tank_group_heat_flux_jacobi[_qp] = OT_heat_flux_jacobi_list[findi];
	}
}
